package cn.itcast.login;

import cn.itcast.base.BaseController;
import cn.itcast.domain.module.Module;
import cn.itcast.domain.user.User;
import cn.itcast.domain.wxLogin.WXUser;
import cn.itcast.service.system.UserService;
import cn.itcast.service.system.WechartService;
import cn.itcast.utils.commons.HttpUtils;
import cn.itcast.utils.commons.UtilFuns;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Map;


public class WxLoginController extends BaseController {
  /*  @Reference
    private WechartService wechartService;*/
/*
    @Autowired
    private UserService userService;*/

    private String appid = "wx3bdb1192c22883f3";
    private String secret = "db9d6b88821df403e5ff11742e799105";
    private String accessTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token";
    private String wxInfoUrl = "https://api.weixin.qq.com/sns/userinfo";

   /* @RequestMapping(value = "/weixinlogin", name = "进入微信登录")
    public String weixinLogin(String code) {

        if (code == null) {
            System.out.println("未授权");
        }

        String atUtl = accessTokenUrl + "?code=" + code + "&appid=" + appid + "&secret=" + secret + "&grant_type=authorization_code";
        Map<String, Object> map1 = null;
        try {
            map1 = HttpUtils.sendGet(atUtl);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Object access_token = map1.get("access_token");
        Object openid = map1.get("openid").toString();
        String wxurl = wxInfoUrl + "?access_token=" + access_token + "&openid=" + openid;
        Map<String, Object> map2 = null;
        try {
            map2 = HttpUtils.sendGet(wxurl);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Object unionid = map2.get("unionid");
        String unionid_s = unionid.toString();
        User user1 = userService.finbByUnionid(unionid_s);

        if (user1 == null) {
            //微信绑定页面
            request.setAttribute("unionid", unionid_s);
            return "forward:email.jsp";
        }
        //创建令牌
        UsernamePasswordToken token = new UsernamePasswordToken(user1.getEmail(), user1.getPassword());
        //获取主题
        Subject subject = SecurityUtils.getSubject();
        //开始认证
        try {
            subject.login(token);
        } catch (AuthenticationException e) {
            //e.printStackTrace();
            request.setAttribute("error","邮箱或者密码错误");
            return "forward:login.jsp";
        }
        //获取主角
        User user = (User) subject.getPrincipal();//从shiro中获取当前登录人
        //把登录人放入到session
        session.setAttribute("loginUser",user);
        //查询当前用户所具有的权限并返回给前段页面显示
        List<Module> menus = userService.findMenus(user);
        session.setAttribute("modules",menus);
        return "redirect:log.do";
    }

    @RequestMapping(value = "/log", name = "微信进入主页面")
    public String log() {
        return "home/main";
    }

    //微信登陆邮箱绑定
    @RequestMapping(value = "/emailAdd", name = "微信第一次扫码进入绑定邮箱方法")
    public String emailAdd(String email, String password, String unionid) {
        if (UtilFuns.isEmpty(email) || UtilFuns.isEmpty(password)) {
            request.setAttribute("unionid", unionid);
            request.setAttribute("error", "邮箱或密码不能为空");
            return "forward:email.jsp";
        }


        return "redirect:log.do";
    }
*/
}
